﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var rmsg=requestM(textBox1.Text,textBox2.Text,textBox3.Text);
           
            MessageBox.Show(rmsg);
        }

        public static CookieContainer container = null;

        //登录
        public string requestM(string Uname,string passwd,string vaildate)
        {

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create("https://secure.elong.com/passport/ajax/elongLogin");
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3486.0 Safari/537.36";
                request.AllowAutoRedirect = true;
                request.CookieContainer = container;
                request.KeepAlive = true;
                string postData= string.Format("userName={0}&passwd={1}&validateCode={2}&rememberMe=true&token=&loginLevel=2", Uname, passwd, vaildate);
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] bytepostData = encoding.GetBytes(postData);
                request.ContentLength = bytepostData.Length;


                using (Stream requestStm = request.GetRequestStream())
                {
                    requestStm.Write(bytepostData, 0, bytepostData.Length);
                }

                response = (HttpWebResponse)request.GetResponse();
                string text = string.Empty;
                using (Stream responseStm = response.GetResponseStream())
                {

                    StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8);
                    text = redStm.ReadToEnd();
                }

                return text;

            }
            catch (Exception ex)
            {

                var msg = ex.Message;
                return msg;
            }


            
        }


        //获取验证码
        public Stream getCodeStream(string codeUrl)

        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(codeUrl);
            request.Referer = codeUrl;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3486.0 Safari/537.36";
            request.Accept = "image/webp,image/apng,image/*,*/*;q=0.8";
            request.CookieContainer = new CookieContainer();//!Very Important.!!!
            container = request.CookieContainer;
            var c = request.CookieContainer.GetCookies(request.RequestUri);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            response.Cookies = container.GetCookies(request.RequestUri);

            Stream stream = response.GetResponseStream();
            return stream;


           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ReflshPicImage();
        }

        public void ReflshPicImage() {
            string codeUrl = "https://secure.elong.com/passport/getValidateCode";
            Stream stmImage = getCodeStream(codeUrl);
            pictureBox1.Image=Image.FromStream(stmImage);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            ReflshPicImage();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReflshPicImage();
        }
    }
}
